
/*
********* This program cleans survey data for all cities, renames and recodes variables, 
********* generates labels, and saves a final analysis dataset 
********* The program also generates summary statistics in Table A-1 of the appendix 
*/

clear all
set more off

cd "$localdir/Data"
gl output "$localdir/Output"


*** Load raw data
use "STAN0107_OUTPUT_8msas.dta", clear

foreach x of varlist _all {
capture rename `x' `=lower("`x'")'
}



*** Rename and recode variables and make value labels

* Economic evaluations and beliefs
rename q1_all dissatisfied_us_economy
rename q2_all dissatisfied_msa_economy
rename q3_all dissatisfied_personal_finances
drop q1 q1_rev q2 q2_rev q3 q3_rev
rename q4 better_off_than_parents
recode better_off_than_parents 1=5 2=4 3=3 4=2 5=1
lab define agree 1 "Disagree Strongly" 2 "Disagree Somewhat" 3 "Neither Agree nor Disagree" 4 "Agree Somewhat" 5 "Agree Strongly"
lab val better_off_than_parents agree
rename q5 children_better_pros
recode children_better_pros 1=5 2=4 3=3 4=2 5=1
lab val children_better_pros agree
rename q6 changes_msa_worse
rename q10 most_imp_techortrade_msa
rename q11 most_upset_techortrade_msa
rename q12 hard_work_vs_luck_now
rename q13 hard_work_vs_luck_then
rename q14 policy_approach_trade
rename q15 policy_approach_tech
rename q17 inequality_smaller
recode inequality_smaller 1=3 3=2 2=1
lab define ineqsmall 1 "Bigger" 2 "About what it is now" 3 "Smaller" 
lab values inequality_smaller ineqsmall
rename q18 gov_more_safety_net
recode gov_more_safety_net 1=5 2=4 4=2 5=1
lab define safety 1 "Spend much less" 2 "Spend less" 3 "Spend the same" 4 "Spend more" 5 "Spend much more"
lab values gov_more_safety_net safety
rename q19 reduce_trade
recode reduce_trade 6=.
rename q20 for_investment
rename q21 reduce_immig
recode reduce_immig 1=1 2=2 3=3 4=5 5=4  
lab define immigop 1 "Increased a lot" 2 "Increased a little" 3 "Remain the same as it is" 4 "Reduced a little" 5 "Reduced a lot"
lab values reduce_immig immigop
gen attentioncheck=0
replace attentioncheck=1 if q22_test_2==1 & q22_test_3==1 
rename q23 country_dissatisfied
recode country_dissatisfied 1=0 2=1 
lab def satisfaction 0 "Satisfied" 1 "Dissatisfied"
lab val country_dissatisfied satisfaction
rename q24 state_dissatisfied
recode state_dissatisfied 1=0 2=1 
lab val state_dissatisfied satisfaction
rename q25 msa_dissatisfied
recode msa_dissatisfied 1=0 2=1 
lab val msa_dissatisfied satisfaction

* Performance factors
rename q7a imp_perf_factor_trade
recode imp_perf_factor_trade 1=4 2=3 3=2 4=1 
lab define vimp 1 "Not Important At all" 2 "Of Little Importance" 3 "Somewhat Important" 4 "Very Important"
lab values imp_perf_factor_trade vimp

rename q7b imp_perf_factor_tech_change
recode imp_perf_factor_tech_change 1=4 2=3 3=2 4=1
lab values imp_perf_factor_tech_change vimp

rename q7c imp_perf_factor_labor_unions
recode imp_perf_factor_labor_unions 1=4 2=3 3=2 4=1
lab values imp_perf_factor_labor_unions vimp

rename q7d imp_perf_factor_bus_leaders
recode imp_perf_factor_bus_leaders 1=4 2=3 3=2 4=1
lab values imp_perf_factor_bus_leaders vimp

rename q7e imp_perf_factor_bankers
recode imp_perf_factor_bankers 1=4 2=3 3=2 4=1
lab values imp_perf_factor_bankers vimp

rename q7f imp_perf_factor_leaders_wash
recode imp_perf_factor_leaders_wash 1=4 2=3 3=2 4=1
lab values imp_perf_factor_leaders_wash vimp

rename q7g imp_perf_factor_leaders_msa
recode imp_perf_factor_leaders_msa 1=4 2=3 3=2 4=1
lab values imp_perf_factor_leaders_msa vimp

rename q7h imp_perf_factor_mergers
recode imp_perf_factor_mergers 1=4 2=3 3=2 4=1
lab values imp_perf_factor_mergers vimp

rename q7i imp_perf_factor_localecpolicy
recode imp_perf_factor_localecpolicy 1=4 2=3 3=2 4=1
lab values imp_perf_factor_localecpolicy vimp

rename q7j imp_perf_factor_localedpolicy
recode imp_perf_factor_localedpolicy 1=4 2=3 3=2 4=1
lab values imp_perf_factor_localedpolicy vimp

rename q7k imp_perf_factor_localcharities
recode imp_perf_factor_localcharities 1=4 2=3 3=2 4=1
lab values imp_perf_factor_localcharities vimp

rename q7l imp_perf_factor_localhighered
recode imp_perf_factor_localhighered 1=4 2=3 3=2 4=1
lab values imp_perf_factor_localhighered vimp

rename q7m imp_perf_factor_nationalecpolicy
recode imp_perf_factor_nationalecpolicy 1=4 2=3 3=2 4=1
lab values imp_perf_factor_nationalecpolicy vimp

rename q8 most_important_factor_msa

rename q9 reason_most_imp_factor_text

* Trust
rename q26 trust_people
recode trust_people 1=3 2=2 3=1
lab define trustp 1 "Only some of the time" 2 "Most of the time" 3 "Just about always" 
lab values trust_people trustp
rename q27a trust_democraticparty
recode trust_democraticparty 1=4 2=3 3=2 4=1
lab define trust 1 "Trust them not at all" 2 "Trust them only a little" 3 "Trust them some" 4 "Trust them a lot" 
lab values trust_democraticparty trust
rename q27b trust_republicanparty
recode trust_republicanparty 1=4 2=3 3=2 4=1
lab values trust_republicanparty trust
rename q27c trust_polleaders
recode trust_polleaders 1=4 2=3 3=2 4=1
lab values trust_polleaders trust
rename q27d trust_businessleaders
recode trust_businessleaders 1=4 2=3 3=2 4=1
lab values trust_businessleaders trust
rename q27e trust_unionleaders
recode trust_unionleaders 1=4 2=3 3=2 4=1
lab values trust_unionleaders trust
rename q27f trust_companies
recode trust_companies 1=4 2=3 3=2 4=1
lab values trust_companies trust
rename q27g trust_whites
recode trust_whites 1=4 2=3 3=2 4=1
lab values trust_whites trust
rename q27h trust_blacks
recode trust_blacks 1=4 2=3 3=2 4=1
lab values trust_blacks trust
rename q27i trust_latinos
recode trust_latinos 1=4 2=3 3=2 4=1
lab values trust_latinos trust
rename q27j trust_localgovMsa
recode trust_localgovMsa 1=4 2=3 3=2 4=1
lab values trust_localgovMsa trust
rename q27k trust_stategov
recode trust_stategov 1=4 2=3 3=2 4=1
lab values trust_stategov trust
rename q27l trust_fedgov
recode trust_fedgov 1=4 2=3 3=2 4=1
lab values trust_fedgov trust
rename q27m trust_localschools
recode trust_localschools 1=4 2=3 3=2 4=1
lab values trust_localschools trust
rename q27n trust_localcharities
recode trust_localcharities 1=4 2=3 3=2 4=1
lab values trust_localcharities trust
rename q27o trust_Amer_economy
recode trust_Amer_economy 1=4 2=3 3=2 4=1
lab values trust_Amer_economy trust
rename q27p trust_banks
recode trust_banks 1=4 2=3 3=2 4=1
lab values trust_banks trust
rename q27q trust_StateUniv
recode trust_StateUniv 1=4 2=3 3=2 4=1
lab values trust_StateUniv trust
rename q29 fedgov_feel
rename q30 msagov_feel

rename q28a change_trust_democraticparty
recode change_trust_democraticparty 1=1 2=3 3=2 8=.
lab define trust_change 1 "Trust them more" 2 "About the same" 3 "Trust them less" 
lab values change_trust_democraticparty trust_change
rename q28b change_trust_republicanparty
recode change_trust_republicanparty 1=1 2=3 3=2 8=.
lab values change_trust_republicanparty trust_change
rename q28c change_trust_polleaders
recode change_trust_polleaders 1=1 2=3 3=2 8=.
lab values change_trust_polleaders trust_change
rename q28d change_trust_businessleaders
recode change_trust_businessleaders 1=1 2=3 3=2 8=.
lab values change_trust_businessleaders trust_change
rename q28e change_trust_unionleaders
recode change_trust_unionleaders 1=1 2=3 3=2 8=.
lab values change_trust_unionleaders trust_change
rename q28f change_trust_companies
recode change_trust_companies 1=1 2=3 3=2 8=.
lab values change_trust_companies trust_change
rename q28g change_trust_whites
recode change_trust_whites 1=1 2=3 3=2 8=.
lab values change_trust_whites trust_change
rename q28h change_trust_blacks
recode change_trust_blacks 1=1 2=3 3=2 8=.
lab values change_trust_blacks trust_change
rename q28i change_trust_latinos
recode change_trust_latinos 1=1 2=3 3=2 8=.
lab values change_trust_latinos trust_change
rename q28j change_trust_localgovmsa
recode change_trust_localgovmsa 1=1 2=3 3=2 8=.
lab values change_trust_localgovmsa trust_change
rename q28k change_trust_stategov
recode change_trust_stategov 1=1 2=3 3=2 8=.
lab values change_trust_stategov trust_change
rename q28l change_trust_fedgov
recode change_trust_fedgov 1=1 2=3 3=2 8=.
lab values change_trust_fedgov trust_change
rename q28m change_trust_localschools
recode change_trust_localschools 1=1 2=3 3=2 8=.
lab values change_trust_localschools trust_change
rename q28n change_trust_localcharities
recode change_trust_localcharities 1=1 2=3 3=2 8=.
lab values change_trust_localcharities trust_change
rename q28o change_trust_Amer_economy
recode change_trust_Amer_economy 1=1 2=3 3=2 8=.
lab values change_trust_Amer_economy trust_change
rename q28p change_trust_banks
recode change_trust_banks 1=1 2=3 3=2 8=.
lab values change_trust_banks trust_change
rename q28q change_trust_StateUniv
recode change_trust_StateUniv 1=1 2=3 3=2 8=.
lab values change_trust_StateUniv trust_change

foreach x of varlist trust_democraticparty trust_republicanparty trust_polleaders trust_businessleaders ///
trust_unionleaders trust_companies trust_whites trust_blacks trust_latinos trust_localgovMsa trust_stategov ///
trust_fedgov trust_localschools trust_localcharities trust_Amer_economy trust_banks trust_StateUniv {
g high_`x'=(`x'==3 | `x'==4) if `x'!=.
}

foreach x of varlist trust_democraticparty trust_republicanparty trust_polleaders trust_businessleaders ///
trust_unionleaders trust_companies trust_whites trust_blacks trust_latinos trust_localgovMsa trust_stategov ///
trust_fedgov trust_localschools trust_localcharities trust_Amer_economy trust_banks trust_StateUniv {
g vhigh_`x'=.
replace vhigh_`x'=1 if `x'==4
replace vhigh_`x'=0 if `x'==1
}

rename q58 race
g white=(race==1)
g black=(race==2) 
g latino=(race==3)
g other=(race>3)
g other_than_bw=(race>=3)
g non_white=(race!=1)

g whites_high_trust_blacks=high_trust_blacks if white==1
g whites_vhigh_trust_blacks=vhigh_trust_blacks if white==1

lab def temp1 0 "Low Trust Amr Econ" 1 "High Trust Amr Econ"
lab val high_trust_Amer_economy temp1

lab def temp2 0 "Very Low Trust Amr Econ" 1 "Very High Trust Amr Econ"
lab val vhigh_trust_Amer_economy temp2

lab def temp3 0 "Low Trust Local Gov" 1 "High Trust Local Gov"
lab val high_trust_localgovMsa temp3

lab def temp4 0 "Very Low Trust Local Gov" 1 "Very High Trust Local Gov"
lab val vhigh_trust_localgovMsa temp4

lab def temp5 0 "Low Trust Schools" 1 "High Trust Schools"
lab val high_trust_localschools temp5

lab def temp6 0 "Very Low Trust Schools" 1 "Very High Trust Schools"
lab val vhigh_trust_localschools temp6

lab def temp7 0 "Low Trust Uni" 1 "High Trust Uni"
lab val high_trust_StateUniv temp7

lab def temp8 0 "Very Low Trust Uni" 1 "Very High Trust Uni"
lab val vhigh_trust_StateUniv temp8

lab def temp9 0 "Low Trust Charities" 1 "High Trust Charities"
lab val high_trust_localcharities temp9

lab def temp10 0 "Very Low Trust Charities" 1 "Very High Trust Charities"
lab val vhigh_trust_localcharities temp10

lab def temp11 0 "Low Trust Blacks" 1 "High Trust Blacks"
lab val whites_high_trust_blacks temp11

lab def temp12 0 "Very Low Trust Blacks" 1 "Very High Trust Blacks"
lab val whites_vhigh_trust_blacks temp12

egen avr_high_trust_inst=rowmean(high_trust_democraticparty high_trust_republicanparty high_trust_polleaders high_trust_businessleaders high_trust_unionleaders ///
high_trust_companies high_trust_localgovMsa high_trust_stategov high_trust_fedgov high_trust_localschools high_trust_localcharities high_trust_Amer_economy high_trust_banks high_trust_StateUniv)

egen avr_high_trust_inst2=rowmean(high_trust_Amer_economy high_trust_localcharities high_trust_StateUniv high_trust_localschools)

lab def hardwork 1 "Hard work more important" 2 "Equally important" 3 "Luck more important"
lab val hard_work_vs_luck_now hardwork

lab def hardworkthen 1 "Hard work was more important" 2 "Equally important" 3 "Luck was more important"
lab val hard_work_vs_luck_then hardworkthen

* ASC
* Submission
rename q31a believe_leaders
rename q31b leaders_know_best
rename q31c criticize_authorities_rev
recode criticize_authorities_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31d authorities_truthful
rename q31e skeptical_authorities_rev
recode skeptical_authorities_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31f questioning_healthy_rev
recode questioning_healthy_rev (1=5) (2=4) (3=3) (4=2) (5=1)

gen avg_submission = (believe_leaders + leaders_know_best + criticize_authorities_rev + authorities_truthful + skeptical_authorities_rev + questioning_healthy_rev) / 6
alpha believe_leaders leaders_know_best criticize_authorities_rev authorities_truthful skeptical_authorities_rev questioning_healthy_rev, item

* Conventionalism
rename q31g too_much_tradition_rev
recode too_much_tradition_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31h traditions_foundation_society
rename q31i follow_social_traditions
rename q31j traditions_interf_progress_rev
recode traditions_interf_progress_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31k challenge_social_traditions_rev
recode challenge_social_traditions_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31l respect_norms

gen avg_conventionalism = (too_much_tradition_rev + traditions_foundation_society + follow_social_traditions + traditions_interf_progress_rev + challenge_social_traditions_rev + respect_norms) / 6
alpha too_much_tradition_rev traditions_foundation_society follow_social_traditions traditions_interf_progress_rev challenge_social_traditions_rev respect_norms, item

* Aggression
rename q31m force_necessary_groups_threat
rename q31n force_necessary_indv_threat
rename q31o police_avoid_violence_rev
recode police_avoid_violence_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31p people_avoid_violence_rev
recode people_avoid_violence_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31q force_wrong_rev
recode force_wrong_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q31r strong_punishments_necessary

g avg_aggression = (force_necessary_groups_threat + force_necessary_indv_threat + police_avoid_violence_rev + people_avoid_violence_rev + force_wrong_rev + strong_punishments_necessary) / 6
alpha force_necessary_groups_threat force_necessary_indv_threat police_avoid_violence_rev people_avoid_violence_rev force_wrong_rev strong_punishments_necessary, item

gen asc = (avg_aggression + avg_submission + avg_conventionalism) / 3

* Public officials
rename q32 agree_msa_offic_dont_care
recode agree_msa_offic_dont_care 1=5 2=4 3=3 4=2 5=1
*lab define agree 1 "Disagree Strongly" 2 "Disagree Somewhat" 3 "Neither Agree nor Disagree" 4 "Agree Somewhat" 5 "Agree Strongly"
lab values agree_msa_offic_dont_care agree
rename q33 agree_nat_offic_dont_care
recode agree_nat_offic_dont_care 1=5 2=4 3=3 4=2 5=1
lab values agree_nat_offic_dont_care agree
rename q34 agree_dont_have_say_local
recode agree_dont_have_say_local 1=5 2=4 3=3 4=2 5=1
lab values agree_dont_have_say_local agree
rename q35 agree_dont_have_say_nat
recode agree_dont_have_say_nat 1=5 2=4 3=3 4=2 5=1
lab values agree_dont_have_say_nat agree

* Anxiety
rename q36a nervous_anxious
rename q36b not_stop_worrying
rename q36c worry_too_much
rename q36d trouble_relaxing
rename q36e hard_sit_still
rename q36f easily_annoyed
rename q36g awful_might_happen
rename q37 anxiety_impact

* Racial resentment
g racial_resent_1=raciala
recode racial_resent_1 1=5 2=4 3=3 4=2 5=1
g racial_resent_2=racialb
recode racial_resent_2 1=5 2=4 3=3 4=2 5=1
g racial_resent_3=racialc
g racial_resent_4=raciald
g racial_resent=(racial_resent_1 + racial_resent_2 + racial_resent_3 + racial_resent_4)/4

g racial_tolerance_1=raciala
g racial_tolerance_2=racialb
g racial_tolerance_3=racialc
recode racial_tolerance_3 1=5 2=4 3=3 4=2 5=1
g racial_tolerance_4=raciald
recode racial_tolerance_4 1=5 2=4 3=3 4=2 5=1
g racial_tolerance=(racial_tolerance_1 + racial_tolerance_2 + racial_tolerance_3 + racial_tolerance_4)/4

* Demograhics
rename q38_1 divorce
recode divorce 2=0
lab def dummy 1 "Yes" 0 "No"
lab val divorce dummy 

rename q38_2 unemployment
recode unemployment 2=0
lab val unemployment dummy 

rename q38_3 death
recode death 2=0
lab val death dummy 

rename q38_4 alc_drugs
recode alc_drugs 2=0
lab val alc_drugs dummy 

rename q38_5 hospitalized
recode hospitalized 2=0
lab val hospitalized dummy 

rename q38_6 illnes
recode illnes 2=0
lab val illnes dummy 

rename q38_7 none
recode none 2=0
lab val none dummy 

rename q38a people_rely_on

rename q39 other_groups_favored
recode other_groups_favored 1=5 2=4 3=3 4=2 5=1
lab values other_groups_favored agree

rename q40 conservatism
recode conservatism 1=5 2=4 3=3 4=2 5=1
lab define cons 1 "Very liberal" 2 "Moderately liberal" 3 "Middle-of-the-road" 4 "Moderately conservative" 5 "Very conservative"
lab values conservatism cons

rename q41 party_id
recode party_id 4=.
gen party_strong_leaning=1 if party_id==1 & q41_a==1
replace party_strong_leaning=2 if party_id==1 & q41_a==2
replace party_strong_leaning=3 if party_id==3 & q41_b==2
replace party_strong_leaning=4 if party_id==3 & q41_b==3
replace party_strong_leaning=5 if party_id==3 & q41_b==1
replace party_strong_leaning=6 if party_id==2 & q41_a==2
replace party_strong_leaning=7 if party_id==2 & q41_a==1

g democrat=(party_id==1) if party_id!=.
g republican=(party_id==2) if party_id!=.
g independent=(party_id==3) if party_id!=.

g strong_democrat=(party_id==1) if q41_a==1
g strong_republican=(party_id==2) if q41_a==1

g leaning_democrat=.
replace leaning_democrat=1 if (party_id==1 | q41_b==2)
g leaning_republican=.
replace leaning_republican=1 if (party_id==2 | q41_b==1)
replace leaning_democrat=0 if leaning_republican==1
replace leaning_republican=0 if leaning_democrat==1

rename q42 turnout
g did_vote=(turnout==2)
rename q43 vote
g clinton=(vote==1) if vote!=.
g trump=(vote==2) if vote!=.
g other_candidate=(vote==3) if vote!=.
rename q43_other vote_other_txt

rename q44 county
rename q45 zipcode
rename inputstate state

rename q47 birthyear
g age=2017-birthyear

g years_in_msa=age if q46==1
replace years_in_msa=1 if q46==2
replace years_in_msa=q46_other if q46==3

gen agelt30=0 if age!=.
replace agelt30=1 if age<=30 & age!=.
gen age3150=0 if age!=.
replace age3150=1 if age>30 & age<=50 & age!=.
gen age5165=0 if age!=.
replace age5165=1 if age>50 & age<=65 & age!=.
gen agegt65=0 if age!=.
replace agegt65=1 if age>65 & age!=.

rename q48 placeofbirth

g us_citizen=(q49==1)

rename q50 gender
g female=1 if gender==2
replace female=0 if gender==1
lab def gender 0 "Male" 1 "Female"
lab val female gender

rename q51 educ
g university_educ=(educ>=7 & educ<=10)
g above_hs_educ=(educ>=5 & educ<=6)
g college=(educ>=6)
g somecollege=(educ>=5)

rename q52 marital_status
g couple=(marital_status==1 | marital_status==2 | marital_status==7)

g children=(q53==1)
lab def children 0 "No children" 1 "Children"
lab val children children

rename q54 numberofchildren

forval i=1/8 {
rename q55_`i'_age child`i'_age 
rename q55_`i'_gender child`i'_gender
}

g single=(marital_status==3 | marital_status==4 | marital_status==5 | marital_status==6)
lab def single 0 "Couple" 1 "Single"
lab val single single

rename q56 own_where_living  

rename q57 home_value_decreased

rename q59 empl_status
g employed=(empl_status>=1 & empl_status<=3)
g laborforce=(empl_status==1 | empl_status==2 | empl_status==3 | empl_status==4)

rename q60 years_workforce

g years_not_wf=age-25-years_workforce
lab var years_not_wf "Years not in the workforce"

g not_wf_history=(years_not_wf>0) & years_not_wf!=.
lab var not_wf_history "History of not in workforce"
lab def wf 0 "Always in workforce" 1 "Sometimes not in workforce"
lab val not_wf_history wf

rename q61 union_member

rename q62 income

rename q63 major_issues_msa_text

rename q64 things_to_be_done_text

rename q65 reasons_overlooked_text

* Survey
g length=endtime-starttime

rename q66 survey_rating
rename q67 survey_comments_open

gen freqweight=round(weight*10)

* Labels for new variables
lab var avg_submission "Average submission" 
lab var avg_conventionalism "Average conventionalism"
lab var avg_aggression "Average agression"
lab var asc "Average ASC"
lab var reduce_trade "Trade should be reduced or kept at current level"
lab var did_vote "Voted at last election"
lab var clinton "Voted for Hillary Clinton at last election"
lab var trump "Voted for Donald Trump at last election"
lab var other_candidate "Voted for other candidate at last election"
lab var county "County id"
lab var age "Age"
lab var female "Female"
lab var university_educ "University degree" 
lab var above_hs_educ "Some college or more education"
lab var college "College degree"
lab var somecollege "Some College"
lab var couple "Couple, married or domestic partnership"
lab var employed "In employment"
lab var laborforce "In the laborforce"
lab var attentioncheck "Attention Check Passed=1"
lab var democrat "Identifies as a Democrat"
lab var republican "Identifies as a Republican"
lab var strong_democrat "Strong Democrat"
lab var strong_republican "Strong Republican"
lab var leaning_democrat "Leaning Democrat"
lab var leaning_republican "Leaning Republican"
lab var years_in_msa "Number of years lived in msa"
lab var white "Respondent identifies as white"
lab var black "Respondent identifies as black"
lab var latino "Respondent identifies as latino"
lab var other "Respondent identifies as other"
lab var racial_resent "Racial resentment"
lab var racial_tolerance "Racial tolerance"
lab var party_strong_leaning "Scale of how strong a republican" 
lab var us_citizen "US citizen"
lab var children "Do have children"
lab var most_important_factor_msa "Most Important Factor MSA Performance"
lab var avr_high_trust_inst "Average High Trust in Institutions"
lab var avr_high_trust_inst "Average Low Trust in Institutions v2"
lab var better_off_than_parents "Better off than parents"
lab var children_better_pros "Children have better prospects"
lab var hard_work_vs_luck_now "Hard work or luck more important now"
lab var hard_work_vs_luck_then "Hard work or luck more important then"
lab var other_groups_favored "Other racial groups are favored"

g high_growth=(msa==1 | msa==3 | msa==4 | msa==8)
lab def highlow 0 "Low-Growth MSAs" 1 "High-Growth MSAs"
lab val high_growth highlow

g high_trust_people=(trust_people==2 | trust_people==3) if trust_people!=.
lab var high_trust_people "High Trust in People"
lab def trustpeople 0 "Low Trust People" 1 "High Trust People"
lab val high_trust_people trustpeople

* Groups defined by distribution of outcome
pctile temp=asc, n(2)
egen med_asc=total(temp)
drop temp
pctile temp=racial_tolerance, n(2)
egen med_racial_tolerance=total(temp)
drop temp
pctile temp=avr_high_trust_inst, n(2)
egen med_high_trust_inst=total(temp)
drop temp
pctile temp=income, n(2)
egen med_income=total(temp)
drop temp

g above_med_asc=(asc>=med_asc) & asc!=.
g above_med_racial_tolerance=(racial_tolerance>=med_racial_tolerance) & racial_tolerance!=.
g above_med_high_trust_inst=(avr_high_trust_inst>=med_high_trust_inst) & avr_high_trust_inst!=.
g above_med_income=(income>=med_income) & income!=.

* Tercile 1 vs 3
xtile terc_high_trust_inst=avr_high_trust_inst, nq(3)
g terc13_high_trust_inst=.
replace terc13_high_trust_inst=1 if terc_high_trust_inst==3
replace terc13_high_trust_inst=0 if terc_high_trust_inst==1 

xtile terc_income=income, nq(3)
g terc13_income=.
replace terc13_income=1 if terc_income==3
replace terc13_income=0 if terc_income==1

* Quartile 1 vs 4
xtile quar_high_trust_inst=avr_high_trust_inst, nq(4)
g quar14_high_trust_inst=.
replace quar14_high_trust_inst=1 if quar_high_trust_inst==4
replace quar14_high_trust_inst=0 if quar_high_trust_inst==1

* Value labels
lab def med_asc 0 "Below-Median ASC" 1 "Above-Median ASC"
lab val above_med_asc med_asc

lab def med_racial 0 "Below-Median Racial Tolerance" 1 "Above-Median Racial Tolerance"
lab val above_med_racial_tolerance med_racial

lab def med_trustinst 0 "Below-Median High Trust Institutions" 1 "Above-Median High Trust Institutions"
lab val above_med_high_trust_inst med_trustinst

lab def med_income 0 "Below-Median Income" 1 "Above-Median Income"
lab val above_med_income med_income

lab def terc13 0 "1st Terc Trust Inst" 1 "3rd Terc Trust Inst"
lab val terc13_high_trust_inst terc13

lab def terc13Income 0 "1st Terc Income" 1 "3rd Terc Income"
lab val terc13_income terc13Income

lab def quar14 0 "1st Quar Trust Inst" 1 "4rd Quar Trust Inst"
lab val quar14_high_trust_inst quar14

lab def college 0 "Less than College" 1 "College or More"
lab val college college

* Shorter labels for graph-file names (cannot be too long)
g above_med_asc_copy=above_med_asc
g above_med_racial_tolerance_copy=above_med_racial_tolerance
g above_med_high_trust_inst_copy=above_med_high_trust_inst

lab def med_asc2 0 "Low ASC" 1 "High ASC"
lab val above_med_asc_copy med_asc2

lab def med_tolerance2 0 "Low Racial Tolerance" 1 "High Racial Tolerance"
lab val above_med_racial_tolerance_copy med_racial2

lab def med_trustinst2 0 "Low High Trust Inst" 1 "High High Trust Inst"
lab val above_med_high_trust_inst_copy med_trustinst2



*** Save analysis data
save msa_survey_indiv, replace






****** Summary statistics by MSA ******
 
foreach i of numlist 1/8 {

use msa_survey_indiv, clear

keep if msa==`i'
tab msa

local forlab: value label msa
local label: label `forlab' `i'
di "`label'"

*log using "$output/sumstat_`label'.smcl", replace

tabstat age female white black latino college somecollege laborforce democrat republican clinton trump, s(mean n) c(s)
tabstat age female white black latino college somecollege laborforce democrat republican clinton trump [fweight=freqweight], s(mean n) c(s)

*log close
}







